package com.axend.vetwavve.activity

import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.os.Bundle
import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.FragmentManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.android.jws.JwsManager
import com.axend.lib_base.base.activity.BaseVVDActivity
import com.axend.lib_base.base.vm.BaseViewModel
import com.axend.lib_base.ext.addFragment
import com.axend.lib_base.help.otgserial.SerialManage
import com.axend.lib_base.utils.AudioPlayer
import com.axend.lib_base.utils.ScreenAutoAdapter
import com.axend.lib_service.ARouterPath
import com.axend.vetwavve.databinding.ActivityMainBinding
import com.axend.vetwavve.fragment.DeskTopFragment
import java.util.logging.Logger

/**
 * fragment管理主页
 */
@Route(path = ARouterPath.ACTIVITY_MAIN)
class MainActivity : BaseVVDActivity<BaseViewModel, ActivityMainBinding>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        val configuration: Configuration = resources.configuration
        if (configuration.orientation === Configuration.ORIENTATION_LANDSCAPE) {
            ScreenAutoAdapter.match(this, 1920f)
        } else if (configuration.orientation === Configuration.ORIENTATION_PORTRAIT) {
            ScreenAutoAdapter.match(this, 1200f)
        }
        requestedOrientation= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        super.onCreate(savedInstanceState)
        // 隐藏虚拟导航栏
        val uiOptions = (View.SYSTEM_UI_FLAG_LOW_PROFILE
                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
        window.decorView.systemUiVisibility = uiOptions
    }

    override fun init(savedInstanceState: Bundle?) {
        /*监听fragment回退栈事件*/
        supportFragmentManager.addOnBackStackChangedListener(getFragmentStackListener())
        addFragment(mBinding.fcvMainContent.id, DeskTopFragment(), null, null, false)


        // 添加回调到OnBackPressedDispatcher
        onBackPressedDispatcher.addCallback(this, onListenerCallBack)
    }


    /**
     * 回退栈改变事件
     */
    private fun getFragmentStackListener(): FragmentManager.OnBackStackChangedListener {
        return FragmentManager.OnBackStackChangedListener {
            supportFragmentManager?.let {
                it.findFragmentById(mBinding.fcvMainContent.id)?.onResume()
            }
        }
    }


    /**
     * 回退监听
     */
    private val onListenerCallBack: OnBackPressedCallback = object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            Logger.getLogger("MainActivity").info("handleOnBackPressed")
            onBack()
        }
    }


    fun onBack(){
        if (supportFragmentManager.backStackEntryCount == 0) {
            finish()
            return
        }
        supportFragmentManager.popBackStack()
    }


    /**
     * 获取fragment容器值
     */
    fun getFragmentContainerId(): Int = mBinding.fcvMainContent.id


    override fun onResume() {
        super.onResume()
        requestedOrientation= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
    }

    override fun onDestroy() {
        super.onDestroy()
        onListenerCallBack.remove()
        val jwsManager = JwsManager.create(this)
        jwsManager.jwsSetStatusBar(this,true)
//        SerialManage.getInstance().colse()
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
    }




}

